﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using ScrumTime.Models;

namespace ScrumTime.Helpers
{
    public class Users
    {
        static Users _Instance;
       // List<User> _Users;

        ScrumTimeEntities _ScrumTimeEntities;

        private Users()
        {

           
            _ScrumTimeEntities = new ScrumTimeEntities();

        //    _Users = ScrumTime.Services.UserService.GetAllUsabilityFeatures(_ScrumTimeEntities);
        }


        private bool hasThisRol(int rolId, User user)
        {
            bool resultado = false;
            foreach (ScrumTime.Models.Role rol in user.Roles) {
                if (rol.RolId == rolId) {
                    resultado = true;
                    break;
                }
            }
            return resultado;
        }

        private bool _IsUserAdmin(String userId) {
            bool resultado = false;
            User user = ScrumTime.Services.UserService.GetUserByGUID(_ScrumTimeEntities, userId);
            if (user != null) {
                resultado = hasThisRol(ScrumTime.Helpers.ScrumTimeAppConfig.GetUserId_Admin(), user);
            }
            return resultado;
        }


        private bool _IsUserScrumMasterOrAdmin(String userId)
        {
            bool resultado = false;
            User user = ScrumTime.Services.UserService.GetUserByGUID(_ScrumTimeEntities, userId);
            if (user != null)
            {
                resultado = hasThisRol(ScrumTime.Helpers.ScrumTimeAppConfig.GetUserId_Admin(), user)
                            ||
                            hasThisRol(ScrumTime.Helpers.ScrumTimeAppConfig.GetUserId_ScrumMaster(), user);
            }
            return resultado;
        }

        public static bool IsUserAdmin( String userId){
            if (_Instance == null)
                _Instance = new Users();
            return _Instance._IsUserAdmin(userId);
        }


        public static bool IsUserAdminOrScrumMaster( String userId){
            if (_Instance == null)
                _Instance = new Users();
            return _Instance._IsUserScrumMasterOrAdmin(userId);
        }
        


    }
}